#include "CgiEnvironment.h"

std::string CgiEnvironment::getEnvVar(const std::string &nombre) const {
	char* envVar = getenv(nombre.c_str());
	if(envVar != NULL)
		return (std::string(envVar));
	return std::string();
}

long CgiEnvironment::getEnvVarLong(const std::string &nombre) const {
	long valor;
	std::string varLeida = this->getEnvVar(nombre);
	if(!varLeida.empty() && sscanf(varLeida.c_str(), "%ld", &valor) == 1)
		return valor;
	return -1;
}

void CgiEnvironment::setEnvVar(std::string &envVar,
		const std::string &nombre, const std::string &valor) {
	// Armo la cadena con formato nombre=valor
	envVar.assign(nombre);
	envVar.append("=");
	envVar.append(valor);
	if(putenv((char*) envVar.c_str()) != 0)
		throw std::exception();
}

void CgiEnvironment::setEnvVarLong(std::string &envVar,
		const std::string &nombre, long valor) {
	std::stringstream ss;
	ss << valor;
	this->setEnvVar(envVar, CONTENT_LENGTH, ss.str());
}

std::string CgiEnvironment::getAUTH_TYPE() const {
    return this->getEnvVar(AUTH_TYPE);
}

long CgiEnvironment::getCONTENT_LENGTH() const {
	return this->getEnvVarLong(CONTENT_LENGTH);
}

std::string CgiEnvironment::getCONTENT_TYPE() const {
    return this->getEnvVar(CONTENT_TYPE);
}

std::string CgiEnvironment::getGATEWAY_INTERFACE() const {
    return this->getEnvVar(GATEWAY_INTERFACE);
}

std::string CgiEnvironment::getPATH_INFO() const {
    return this->getEnvVar(PATH_INFO);
}

std::string CgiEnvironment::getPATH_TRANSLATED() const {
    return this->getEnvVar(PATH_TRANSLATED);
}

std::string CgiEnvironment::getQUERY_STRING() const {
    return this->getEnvVar(QUERY_STRING);
}

std::string CgiEnvironment::getREMOTE_ADDR() const {
    return this->getEnvVar(REMOTE_ADDR);
}

std::string CgiEnvironment::getREMOTE_HOST() const {
    return this->getEnvVar(REMOTE_HOST);
}

std::string CgiEnvironment::getREMOTE_IDENT() const {
    return this->getEnvVar(REMOTE_IDENT);
}

std::string CgiEnvironment::getREMOTE_USER() const {
    return this->getEnvVar(REMOTE_USER);
}

std::string CgiEnvironment::getREQUEST_METHOD() const {
    return this->getEnvVar(REQUEST_METHOD);
}

std::string CgiEnvironment::getSCRIPT_NAME() const {
    return this->getEnvVar(SCRIPT_NAME);
}

std::string CgiEnvironment::getSERVER_NAME() const {
    return this->getEnvVar(SERVER_NAME);
}

long CgiEnvironment::getSERVER_PORT() const {
    return this->getEnvVarLong(SERVER_PORT);
}

std::string CgiEnvironment::getSERVER_PROTOCOL() const {
    return this->getEnvVar(SERVER_PROTOCOL);
}

std::string CgiEnvironment::getSERVER_SOFTWARE() const {
    return this->getEnvVar(SERVER_SOFTWARE);
}

std::string CgiEnvironment::getDOCUMENT_ROOT() const {
	return this->getEnvVar(DOCUMENT_ROOT);
}

std::string CgiEnvironment::getREDIRECT_STATUS() const {
	return this->getEnvVar(REDIRECT_STATUS);
}

void CgiEnvironment::setAUTH_TYPE(const std::string &valor) {
	this->setEnvVar(this->authType, AUTH_TYPE, valor);
}

void CgiEnvironment::setCONTENT_LENGTH(long valor) {
	this->setEnvVarLong(this->contentLength, CONTENT_LENGTH, valor);
}

void CgiEnvironment::setCONTENT_TYPE(const std::string &valor) {
	this->setEnvVar(this->contentType, CONTENT_TYPE, valor);
}

void CgiEnvironment::setGATEWAY_INTERFACE(const std::string &valor) {
	this->setEnvVar(this->gatewayInterface, GATEWAY_INTERFACE, valor);
}

void CgiEnvironment::setPATH_INFO(const std::string &valor) {
	this->setEnvVar(this->pathInfo, PATH_INFO, valor);
}

void CgiEnvironment::setPATH_TRANSLATED(const std::string &valor) {
	this->setEnvVar(this->pathTranslated, PATH_TRANSLATED, valor);
}

void CgiEnvironment::setQUERY_STRING(const std::string &valor) {
	this->setEnvVar(this->queryString, QUERY_STRING, valor);
}

void CgiEnvironment::setREMOTE_ADDR(const std::string &valor) {
	this->setEnvVar(this->remoteAddr, REMOTE_ADDR, valor);
}

void CgiEnvironment::setREMOTE_HOST(const std::string &valor) {
	this->setEnvVar(this->remoteHost, REMOTE_HOST, valor);
}

void CgiEnvironment::setREMOTE_IDENT(const std::string &valor) {
	this->setEnvVar(this->remoteIdent, REMOTE_IDENT, valor);
}

void CgiEnvironment::setREMOTE_USER(const std::string &valor) {
	this->setEnvVar(this->remoteUser, REMOTE_USER,valor);
}

void CgiEnvironment::setREQUEST_METHOD(const std::string &valor) {
	this->setEnvVar(this->requestMethod, REQUEST_METHOD, valor);
}

void CgiEnvironment::setSCRIPT_NAME(const std::string &valor) {
	this->setEnvVar(this->scriptName, SCRIPT_NAME, valor);
}

void CgiEnvironment::setSERVER_NAME(const std::string &valor) {
	this->setEnvVar(this->serverName, SERVER_NAME, valor);
}

void CgiEnvironment::setSERVER_PORT(long valor) {
	this->setEnvVarLong(this->serverPort, SERVER_PORT, valor);
}

void CgiEnvironment::setSERVER_PROTOCOL(const std::string &valor) {
	this->setEnvVar(this->serverProtocol, SERVER_PROTOCOL, valor);
}

void CgiEnvironment::setSERVER_SOFTWARE(const std::string &valor) {
	this->setEnvVar(this->serverSoftware, SERVER_SOFTWARE, valor);
}

void CgiEnvironment::setDOCUMENT_ROOT(const std::string &valor) {
	this->setEnvVar(this->documentRoot, DOCUMENT_ROOT, valor);
}

void CgiEnvironment::setREDIRECT_STATUS(const std::string &valor) {
	this->setEnvVar(this->redirectStatus, REDIRECT_STATUS, valor);
}
